//
//  AllMusicViewController.m
//  悦享
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "AllMusicViewController.h"
#import "AllMusicModel.h"
#import "AllMusicCollectionReusableView.h"
#import "ChildrenModel.h"
#import "AllMusicCollectionViewCell.h"

@interface AllMusicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

/// 保存所有数据的大数组
@property (nonatomic, retain) NSMutableArray *bigArr;
@property (nonatomic, retain) UICollectionView *allMusicCollection;
/// 保存分区数据的数组
@property (nonatomic, retain) NSMutableArray *childrenArr;

@end

@implementation AllMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.bigArr = [NSMutableArray array];
    self.childrenArr = [NSMutableArray array];
    
    [self createViews];
    
    [self dataHandle];
}

#pragma mark - 创建UICollectionView
- (void)createViews {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    self.allMusicCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SREEN_WIDTH, SREEN_HEIGHT - 164) collectionViewLayout:layout];
    _allMusicCollection.delegate = self;
    _allMusicCollection.dataSource = self;
    _allMusicCollection.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_allMusicCollection];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 50)];
    titleLabel.backgroundColor = [UIColor lightGrayColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"全部";
    titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLabel];
    
    [_allMusicCollection registerClass:[AllMusicCollectionViewCell class] forCellWithReuseIdentifier:@"AllMusicCollectionViewCell"];
    
    // 注册透视图
    [_allMusicCollection registerClass:[AllMusicCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}


#pragma mark - UICollectionView Delegate & DataSource
// 每个分区item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        return CGSizeMake((SREEN_WIDTH - 25) / 4, 50);
  
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    AllMusicModel *model = _bigArr[section];
    
    
        return model.childrenArr.count;
   
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AllMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AllMusicCollectionViewCell" forIndexPath:indexPath];

    
    AllMusicModel *model = _bigArr[indexPath.section];
    ChildrenModel *smallModel = model.childrenArr[indexPath.row];
    
    cell.model = smallModel;
    

    return cell;
    
}

#pragma mark collectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AllMusicModel *model = _bigArr[indexPath.section];
    ChildrenModel *smallModel = model.childrenArr[indexPath.row];
    self.block(smallModel.tag);
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return _bigArr.count;
    
}

#pragma mark 分区头赋值
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        
        AllMusicCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        AllMusicModel *model = _bigArr[indexPath.section];
        header.nameString = model.tag;
        
    
        return header;
    }
    return nil;
}
#pragma mark header大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

        
        return CGSizeMake(SREEN_WIDTH, 30);
    
    
}
#pragma mark - 数据处理
- (void)dataHandle {
    
    NSString *path = [NSString stringWithFormat:@"http://api.dongting.com/song/tag/songlist?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude="];
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {
        
        NSArray *tempArr = [AllMusicModel baseModelHandleWithArr:responseObject[@"data"]];
        [self.bigArr addObjectsFromArray:tempArr];
        
        [_allMusicCollection reloadData];
        
//        NSLog(@"%ld", _bigArr.count);
        
    } filed:^(NSError *error) {
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
